package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * ClassName:NursingServiceImpl
 * package:com.zzyl.service.impl
 * description:
 *
 * @author 刘
 * @version 1.0
 * @Create 2024/5/31 14:47
 */
@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;


    /**
     * 查询所有计划
     * @return
     */
    @Override
    public List<NursingPlanVo> getAllNursingPlan() {
        List<NursingPlanVo> nursingPlanVo = nursingPlanMapper.selectAllPlan();

        return nursingPlanVo;
    }

    /**
     * 新增护理计划
     * @param nursingPlanDto
     */
    @Override
    @Transactional
    public void addNursingPlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);

        nursingPlan.setStatus(1);

        nursingPlanMapper.insertNursingPlan(nursingPlan);

        //主键返回，planId

//        for (NursingProjectPlan projectPlan : nursingPlanDto.getProjectPlans()) {
//            // 设置NursingProjectPlan的planId
//            projectPlan.setPlanId(nursingPlan.getId().intValue());
//        }
        nursingPlanDto.getProjectPlans().forEach(eachData -> eachData.setPlanId(nursingPlan.getId().intValue()));


        nursingPlanMapper.insertNursingProjectPlan(nursingPlanDto.getProjectPlans());
    }

    /**
     * 分页查询
     * @param name
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @Override
    public PageResponse<NursingPlanVo> getByLimit(String name, Integer pageNum, Integer pageSize, Integer status) {

        PageHelper.startPage(pageNum,pageSize);
        Page<NursingPlanVo> page =  nursingPlanMapper.selectByLimit(name,status);

        PageResponse<NursingPlanVo> pageResponse = PageResponse.of(page, NursingPlanVo.class);

        return pageResponse;
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    @Transactional
    public NursingPlanVo getById(Integer id) {

//        NursingPlanVo nursingPlanVo = nursingPlanMapper.selectById(id);

        //先查nursingProjectPlan表数据projectPlans
        List<NursingProjectPlanVo> projectPlans = nursingPlanMapper.selectProjectPlans(id);

        //再查询nursingPlan表数据
        NursingPlanVo nursingPlanVo = nursingPlanMapper.selectPlanById(id);

        //把projectPlans放到nursingPlanVo里面
        nursingPlanVo.setProjectPlans(projectPlans);

        return nursingPlanVo;

    }

    /**
     * 编辑修改
     * @param id
     * @param nursingPlanDto
     */
    @Override
    @Transactional
    public void updateById(Integer id, NursingPlanDto nursingPlanDto) {
        //先修改NursingPlanDto除projectPlans的内容
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);

        nursingPlanMapper.updateById(nursingPlan);
        //再删除所有此计划下的项目
        nursingPlanMapper.deleteProjectPlan(id);
        //最后在此计划下新增项目
        nursingPlanDto.getProjectPlans().forEach(eachData -> eachData.setPlanId(id));

        nursingPlanMapper.insertNursingProjectPlan(nursingPlanDto.getProjectPlans());
    }

    /**
     * 删除
     * @param id
     */
    @Override
    @Transactional
    public void deleteById(Integer id) {
        //1、删除计划表中数据
        nursingPlanMapper.deletePlan(id);
        //2、删除中间表中数据
        nursingPlanMapper.deleteProjectPlan(id);
    }

    /**
     * 启用禁用
     * @param id
     * @param status
     */
    @Override
    public void updateStatusById(Integer id, Integer status) {
        nursingPlanMapper.updateStatusById(id,status);
    }
}
